package com.xuelangyun.form.config;

import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;

import javax.servlet.MultipartConfigElement;
import java.io.File;

/**
 * 扩展实现多文件上传配置
 */
//@Configuration
public class MultipartFileConfig {

    //	@Value("${tr.upload.temp}")
    private String tmpPath;

    /**
     * 文件上传临时路径
     */
    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        if (tmpPath == null || tmpPath.trim().isEmpty() || !new File(tmpPath).exists()) {
            tmpPath = System.getProperty("java.io.tmpdir");
        }
        factory.setLocation(tmpPath);
        // "10Mb"
        factory.setMaxFileSize(DataSize.of(10, DataUnit.MEGABYTES));
        factory.setMaxRequestSize(DataSize.of(10, DataUnit.MEGABYTES));
        return factory.createMultipartConfig();
    }
}
